package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import com.google.gson.Gson;

import sz.szsmk.citizencard.model.network.bean.request.AdvertisementsReq;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.GetUserInfoRes;
import sz.szsmk.citizencard.model.network.dzbean.res.User;
import sz.szsmk.citizencard.mvp.PersonInfoQueryMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

import rx.functions.Action1;

/**
 * Created by chensf on 17/10/4.
 */

public class PersonInfoQueryPresenter<V extends PersonInfoQueryMvpView> extends AppBasePresenter<V> {
    NetworkDateSource mNetworkDateSource;

    public PersonInfoQueryPresenter(){
        mNetworkDateSource = NetworkDateSource.getInstance();
    }

    public void getPersonInfo(){
        checkViewAttached();
        HttpApi httpApi = mNetworkDateSource.getHttpApi();
        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();

//
//        httpApi.getUserInfo(commonRequest).compose(RxNetworkHelp.<User>handleResult()).subscribe(new RxSubscribe<User>(getActivity()) {
//            @Override
//            protected void onOk(User response) {
//                getMvpView().onGainPersonInfo(response);
//            }
//        });
//
        mNetworkDateSource.getUserInfo(commonRequest, new Action1<GetUserInfoRes>() {
            @Override
            public void call(GetUserInfoRes baseRes) {
                String code = baseRes.getCode();
                if (code.equals("0000")){
                    Object data = baseRes.getData();
                    String json = new Gson().toJson(data);
                    User user = new Gson().fromJson(json, User.class);
                    getMvpView().onGainPersonInfo(baseRes);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void getAdvertisements(){
        AdvertisementsReq advertisementsReq = new AdvertisementsReq();
        advertisementsReq.setAdArea("2");

        mNetworkDateSource.getAdvertisements( advertisementsReq, baseRes -> {
            String code = baseRes.getCode();
            if (code.equals("0000")) {
                getMvpView().getAdvertisementsResult(baseRes);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
